package com.bestplus.daqingapp;

import android.app.Application;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bestplus.daqingapp.common.CommonPackage;
import com.bestplus.daqingapp.common.step.Pedometer;
import com.bestplus.daqingapp.daemonprocess.AssistantReceiver;
import com.bestplus.daqingapp.daemonprocess.AssistantService;
import com.bestplus.daqingapp.daemonprocess.DaemonReceiver;
import com.bestplus.daqingapp.daemonprocess.DaemonService;
import com.brentvatne.react.ReactVideoPackage;
import com.cboy.rn.splashscreen.SplashScreenReactPackage;
import com.dskj.dscj.aliyunoss.aliyunossPackage;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.github.yamill.orientation.OrientationPackage;
import com.imagepicker.ImagePickerPackage;
import com.marswin89.marsdaemon.DaemonApplication;
import com.marswin89.marsdaemon.DaemonConfigurations;
import com.rnim.rn.audio.ReactNativeAudioPackage;
import com.theweflex.react.WeChatPackage;
import com.RNFetchBlob.RNFetchBlobPackage;
import java.util.Arrays;
import java.util.List;
import com.zmxv.RNSound.RNSoundPackage;
import com.pgyersdk.crash.PgyCrashManager;
import cn.jpush.reactnativejpush.JPushPackage;
public class MainApplication extends DaemonApplication implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    private boolean SHUTDOWN_TOAST = false;
    private boolean SHUTDOWN_LOG = false;
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new RNFetchBlobPackage(),
          new WeChatPackage(),
          new OrientationPackage(),
          new ImagePickerPackage(),
          new aliyunossPackage(),
          new ReactNativeAudioPackage(),
          new RNSoundPackage(),
          new SplashScreenReactPackage(),
          new ReactVideoPackage(),
          new CommonPackage(),
          new JPushPackage(SHUTDOWN_TOAST, SHUTDOWN_LOG)
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }


  @Override
  public void onCreate() {
    super.onCreate();
    startService(new Intent(this, DaemonService.class));
    Pedometer.start(this);
    SoLoader.init(this, /* native exopackage */ false);
    PgyCrashManager.register(this);
  }

   @Override
  protected DaemonConfigurations getDaemonConfigurations() {
    DaemonConfigurations.DaemonConfiguration configuration1 =
            new DaemonConfigurations.DaemonConfiguration("com.bestplus.daqingapp:daemon",
                    DaemonService.class.getCanonicalName(), DaemonReceiver.class.getCanonicalName());
    DaemonConfigurations.DaemonConfiguration configuration2 =
            new DaemonConfigurations.DaemonConfiguration("com.bestplus.daqingapp:assistant",
                    AssistantService.class.getCanonicalName(), AssistantReceiver.class.getCanonicalName());
    DaemonConfigurations.DaemonListener listener = new InnerDaemonListener();
    return new DaemonConfigurations(configuration1, configuration2, listener);
  }

  private static class InnerDaemonListener implements DaemonConfigurations.DaemonListener {

    @Override
    public void onPersistentStart(Context context) {
      Log.i("log", "onPersistentStart: ");
      Pedometer.start(context);
    }

    @Override
    public void onDaemonAssistantStart(Context context) {
      Log.i("log", "onDaemonAssistantStart: ");
    }

    @Override
    public void onWatchDaemonDaed() {

    }
  }
}
